<?php
/**
 * author: crisen
 * email: crisen@crisen.org
 * date: 18-12-24
 * description:
 */

namespace App\Admin\Extensions;

use Encore\Admin\Grid\Displayers\Actions as BaseActions;
use Encore\Admin\Admin;

class Actions extends BaseActions
{

    protected $state = '审核';

    protected $actions = ['view', 'edit', 'state', 'delete'];


    public function state($name)
    {
        $this->state = $name;
    }

    /**
     * Disable view action.
     *
     * @return $this
     */
    public function disableState()
    {
        array_delete($this->actions, 'state');

        return $this;
    }

    /**
     * Disable view action.
     *
     * @return $this
     */
    public function disableCheck()
    {
        array_delete($this->actions, 'check');

        return $this;
    }


    /**
     * Render delete action.
     *
     * @return string
     */
    protected function renderDelete()
    {
        $deleteConfirm = trans('admin.delete_confirm');
        $confirm = trans('admin.confirm');
        $cancel = trans('admin.cancel');

        $script = <<<SCRIPT

$('.{$this->grid->getGridRowName()}-delete').unbind('click').click(function() {

    var id = $(this).data('id');

    swal({
        title: "$deleteConfirm",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "$confirm",
        showLoaderOnConfirm: true,
        cancelButtonText: "$cancel",
        preConfirm: function() {
            return new Promise(function(resolve) {
                $.ajax({
                    method: 'post',
                    url: '{$this->getResource()}/' + id,
                    data: {
                        _method:'delete',
                        _token:LA.token,
                    },
                    success: function (data) {
                        $.pjax.reload('#pjax-container');

                        resolve(data);
                    }
                });
            });
        }
    }).then(function(result) {
        var data = result.value;
        if (typeof data === 'object') {
            if (data.status) {
                swal(data.message, '', 'success');
            } else {
                swal(data.message, '', 'error');
            }
        }
    });
});


SCRIPT;
        Admin::script($script);

        return <<<EOT
        <a  href="javascript:void(0);" data-id="{$this->getKey()}" class="{$this->grid->getGridRowName()}-delete">
           <i class=" btn btn-danger" >删除</i>
        </a>

EOT;

    }


    public function renderState()
    {
        $stateConfirm = trans('确认修改？');
        $confirm = trans('admin.confirm');
        $cancel = trans('admin.cancel');

        $script = <<<SCRIPT
        
        
$('.{$this->grid->getGridRowName()}-state').unbind('click').click(function() {

    var id = $(this).data('id');

    swal({
        title: "$stateConfirm",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "$confirm",
        showLoaderOnConfirm: true,
        cancelButtonText: "$cancel",
        preConfirm: function() {
            return new Promise(function(resolve) {
                $.ajax({
                    method: 'post',
                    url: '{$this->getResource()}/' + id,
                    data: {
                        _method:'put',
                        _token:LA.token,
                        action: 'state'
                    },
                    success: function (data) {
                        $.pjax.reload('#pjax-container');

                        resolve(data);
                    }
                });
            });
        }
    }).then(function(result) {
        var data = result.value;
        if (typeof data === 'object') {
            if (data.status) {
                swal(data.message, '', 'success');
            } else {
                swal(data.message, '', 'error');
            }
        }
    });
});

        
        
SCRIPT;
        Admin::script($script);

        return <<<EOT
        <a href="javascript:void(0);" data-id="{$this->getKey()}" class="{$this->grid->getGridRowName()}-state">
           <i class="btn btn-warning">{$this->state}</i>
        </a>
EOT;
    }


}
